package com.bana.fvs.app.module.detaile;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;

import com.bana.fvs.R;
import com.bana.fvs.app.base.BaseActivity;
import com.bana.fvs.app.base.PageBaseParams;
import com.bana.fvs.app.common.adapter.CommonAdapter;
import com.bana.fvs.app.common.adapter.ViewHolder;
import com.bana.fvs.app.main.entity.FogZoneListItemBean;
import com.bana.fvs.app.module.detaile.entity.AreaControllerDetailBean;
import com.bana.fvs.app.module.detaile.entity.StrategieEntry;
import com.bana.fvs.app.module.detaile.entity.UpdateWorkModeResultBean;
import com.bana.fvs.app.module.detaile.entity.WorkModeItemBean;
import com.bana.fvs.app.mvp.contract.FoglightContract;
import com.bana.fvs.app.mvp.presenter.FoglightPresenter;
import com.bana.fvs.app.util.UserInfoUtils;
import com.bana.fvs.app.view.MyListView;
import com.wei.commonlibrary.net.CommException;
import com.wei.commonlibrary.utils.ToastUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by wei on 2018/2/19.
 * <p>
 * 雾灯控制
 */

public class FogLightControllerActivity extends BaseActivity<FoglightPresenter> implements FoglightContract.IFoglightView {
    private static final String TAG = "dah_FogLightController";

    @BindView(R.id.listView)
    ListView mListView;
    @BindView(R.id.btnCommit)
    TextView mBtnCommit;

    @BindView(R.id.radioGroup)
    RadioGroup radioGroup;
    @BindView(R.id.rb_aotu)
    RadioButton rbAotu;
    @BindView(R.id.rb_hand)
    RadioButton rbHand;
    @BindView(R.id.spinner_lddj)
    Spinner spinner_lddj;
    @BindView(R.id.tv_wdld)
    EditText tvWdld;
    @BindView(R.id.spinner_sspl)
    Spinner spinner_sspl;
    @BindView(R.id.et_ldsc)
    EditText etLdsc;
    @BindView(R.id.et_mdsc)
    EditText etMdsc;
    @BindView(R.id.et_hdys)
    EditText etHdys;
    @BindView(R.id.et_mqsc)
    EditText etMqsc;
    @BindView(R.id.et_hsjsdsl)
    EditText etHsjsdsl;
    @BindView(R.id.et_sdsc)
    EditText etSdsc;
    @BindView(R.id.rb_1)
    RadioButton rb1;
    @BindView(R.id.rb_2)
    RadioButton rb2;
    @BindView(R.id.rb_3)
    RadioButton rb3;

    @BindView(R.id.spinner_3)
    Spinner spinner3;

    @BindView(R.id.spinner_sbkz)
    Spinner spinnerSbkz;
    @BindView(R.id.list_kzcl)
    MyListView listKzcl;
    @BindView(R.id.ll_lddj)
    LinearLayout llLddj;
    @BindView(R.id.ll_wdld)
    LinearLayout llWdld;
    @BindView(R.id.ll_sspl)
    LinearLayout llSspl;
    @BindView(R.id.ll_ldsc)
    LinearLayout llLdsc;
    @BindView(R.id.ll_mdsc)
    LinearLayout llMdsc;
    @BindView(R.id.rg_zkb)
    RadioGroup rgZkb;
    @BindView(R.id.ll_zkb)
    LinearLayout llZkb;
    @BindView(R.id.ll_hdys)
    LinearLayout llHdys;
    @BindView(R.id.ll_mqys)
    LinearLayout llMqys;
    @BindView(R.id.ll_hsjsdsl)
    LinearLayout llHsjsdsl;
    @BindView(R.id.tv_clms)
    TextView tvClms;
    @BindView(R.id.lddj_js)
    TextView lddjJs;
    @BindView(R.id.lddj_zj)
    TextView lddjZj;
    @BindView(R.id.wdld_js)
    TextView wdldJs;
    @BindView(R.id.wdld_zj)
    TextView wdldZj;
    @BindView(R.id.ll_sbkz)
    LinearLayout llSbkz;


    private String remoteWorkModel = "0";

    private List<WorkModeItemBean> datas;
    private int switchModel = 0;
    private CommonAdapter<WorkModeItemBean> adapter;
    private CommonAdapter<WorkModeItemBean> spAdapter;
    private List<WorkModeItemBean> spDatas;
    private int choosedPosition = -1;
    private String wqbm;
    private String lightCode, gzms;
    private FogZoneListItemBean itemBean;

    private List<StrategieEntry> kzclData;
    private CommonAdapter<StrategieEntry> kzclAdapter;

    StrategieEntry strategieEntry;

    @OnClick({R.id.lddj_js, R.id.lddj_zj, R.id.wdld_js, R.id.wdld_zj})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.lddj_js:
                if (spinner_lddj.getSelectedItemPosition() > 0) {
                    spinner_lddj.setSelection(spinner_lddj.getSelectedItemPosition() - 1);
                } else {
                    ToastUtil.showShort(mContext, "已调至最小等级");
                }
                break;
            case R.id.lddj_zj:
                if (itemBean.getSspl() != 1 && spinner_lddj.getSelectedItemPosition() < spDatas.size() - 1) {
                    spinner_lddj.setSelection(spinner_lddj.getSelectedItemPosition() + 1);
                } else if (itemBean.getSspl() == 1 && spinner_lddj.getSelectedItemPosition() < spDatas.size() - 1 && spinner_lddj.getSelectedItemPosition() < 4) {
                    spinner_lddj.setSelection(spinner_lddj.getSelectedItemPosition() + 1);
                } else {
                    ToastUtil.showShort(mContext, "已调至当前模式最大亮度等级");
                }
                break;
            case R.id.wdld_js:
                int i = Integer.parseInt(tvWdld.getText().toString());
                if (i > 1) {
                    i--;
                    tvWdld.setText("" + i);
                } else {
                    ToastUtil.showShort(mContext, "亮度等级最小为1");
                }
                break;
            case R.id.wdld_zj:
                int j = Integer.parseInt(tvWdld.getText().toString());
                if (j < 127) {
                    j++;
                    tvWdld.setText("" + j);
                } else {
                    ToastUtil.showShort(mContext, "亮度最大为127");
                }
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    public class Params extends PageBaseParams {
        public static final String PARAMS_LIGNT_CODE = "_params_lignt_code_";
        public static final String PARAMS_WQ_CODE = "_params_wq_code_";
        public static final String PARAMS_BEAN = "_params_bean_";
        public static final String PARAMS_GZMS = "_params_gzms_";
    }


    public static void startAction(Context context, String lightModel, String typeCode, FogZoneListItemBean itemBean) {
        Intent intent = new Intent(context, FogLightControllerActivity.class);
        intent.putExtra(Params.PARAMS_LIGNT_CODE, lightModel);
        intent.putExtra(Params.PARAMS_WQ_CODE, typeCode);
        intent.putExtra(Params.PARAMS_BEAN, itemBean);
        intent.putExtra(Params.PARAMS_WQ_CODE, itemBean);
        context.startActivity(intent);
    }


    public static void startActionForResult(Activity context, String lightModel, String typeCode, FogZoneListItemBean itemBean) {
        Intent intent = new Intent(context, FogLightControllerActivity.class);
        intent.putExtra(Params.PARAMS_LIGNT_CODE, lightModel);
        intent.putExtra(Params.PARAMS_WQ_CODE, typeCode);
        // intent.putExtra(Params.PARAMS_GZMS, gzms);
        intent.putExtra(Params.PARAMS_BEAN, itemBean);
        context.startActivityForResult(intent, 100);
    }

    @Override
    public int setLayout() {
        return R.layout.activity_foglight_controller;
    }

    @Override
    public void setTitle() {

        mBaseTitleBarView.setTitleText("人工干预");
        mBaseTitleBarView.setLeftTitleGone();
        mBaseTitleBarView.setLeftDrawable(-1);
        lightCode = getIntent().getStringExtra(Params.PARAMS_LIGNT_CODE);
        remoteWorkModel = TextUtils.isEmpty(lightCode) ? "0" : lightCode;//上一页选中的
        wqbm = getIntent().getStringExtra(Params.PARAMS_WQ_CODE);
        // gzms=getIntent().getStringExtra(Params.PARAMS_GZMS);
        itemBean = (FogZoneListItemBean) getIntent().getSerializableExtra(Params.PARAMS_BEAN);
        Log.e(TAG, "remoteWorkModel========" + remoteWorkModel);


    }

    private boolean isclick = true;

    @Override
    public void initWidget() {
        kzclData = new ArrayList<>();
        if (itemBean.getKzcl() > 6 || itemBean.getKzcl() < 1) {
            itemBean.setKzcl(6);
        }
        kzclAdapter = new CommonAdapter<StrategieEntry>(kzclData, R.layout.item_layout_kzcl_list) {
            @Override
            public void convert(ViewHolder helper, StrategieEntry item) {
                RadioButton rb = helper.getView(R.id.rb_kzcl);
                if (item.getClbm() == itemBean.getKzcl()) {
                    strategieEntry = item;
                    rb.setChecked(true);
                    if (isclick)
                        initMode(item);
                    tvClms.setText(item.getClms());
                } else {
                    rb.setChecked(false);
                }
                rb.setText(item.getClmc());
            }
        };

        listKzcl.setAdapter(kzclAdapter);

        listKzcl.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                isclick = true;
                itemBean.setKzcl(kzclData.get(position).getClbm());
                kzclAdapter.notifyDataSetChanged();
            }
        });

        etLdsc.setText(String.valueOf(itemBean.getLdsc()));
        etMdsc.setText(String.valueOf(itemBean.getMdsc()));
        etHdys.setText(String.valueOf(itemBean.getHdys()));
        etMqsc.setText(String.valueOf(itemBean.getMqys()));
        etHsjsdsl.setText(String.valueOf(itemBean.getHsjsdzsl()));
        //etSdsc.setText(String.valueOf(itemBean.getSdsc()));


        mBtnCommit.setOnClickListener(this);
        mBaseTitleBarView.addOnLeftTitleClickListener(this);
        datas = new ArrayList<>();
        spDatas = new ArrayList<>();
        //获取工作模式列表
        String token = UserInfoUtils.getInstance().getToken(mContext);
        // mPresenter.getFoglightMode(this, token);
        mPresenter.http_getStrategies(this, token, new HashMap<>());
        initData();
        mListView.setOnItemClickListener((parent, view, position, id) -> {
            choosedPosition = position;
            datas.get(position).setChoosed(!datas.get(position).isChoosed());
            adapter.notifyDataSetChanged();
        });
        flag = 1;
        initSpinner();
        rgZkb.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_1:
                        itemBean.setZkb(1);
                        break;
                    case R.id.rb_2:
                        itemBean.setZkb(2);
                        break;
                    case R.id.rb_3:
                        itemBean.setZkb(3);
                        break;
                }
            }
        });

    }


    private void initMode(StrategieEntry item) {
        isclick = false;
//        "wdlddjxszt": true,
//                "ssplxszt": true,
//                "zkbxszt": true,
//                "wdkzxszt": false,
//                "wdldxszt": false,
//                "ldscxszt": false,
//                "mdscxszt": false,
//                "hdysxszt": false,
//                "mqysxszt": false,
//                "hsjsdzslxszt": false,
        setLlVisable(llLddj, item.isWdlddjxszt());
        setLlVisable(llSspl, item.isSsplxszt());
        setLlVisable(llZkb, item.isZkbxszt());
        setLlVisable(llWdld, item.isWdldxszt());
        setLlVisable(llLdsc, item.isLdscxszt());
        setLlVisable(llMdsc, item.isMdscxszt());
        setLlVisable(llHdys, item.isHdysxszt());
        setLlVisable(llMqys, item.isMqysxszt());
        setLlVisable(llHsjsdsl, item.isHsjsdzslxszt());
        itemBean.setSspl(item.getSspl());

        if (item.getClbm() == 2 || item.getClbm() == 3) {
            flag = 0;
            initSpinner();
            if (item.getSspl() >= 2)
                spinner_sspl.setSelection(item.getSspl() - 2);
        } else {
            flag = 1;
            initSpinner();
            spinner_sspl.setSelection(0);
        }

        itemBean.setWdkz(item.getWdkz());
        tvWdld.setText(item.getWdld() + "");
        etLdsc.setText(item.getLdsc() + "");
        etMdsc.setText(item.getMdsc() + "");
        etHdys.setText(item.getHdys() + "");
        etMqsc.setText(item.getMqys() + "");
        etHsjsdsl.setText(item.getHsjsdzsl() + "");

        if (item.getZkb() == 1) {
            rb1.setChecked(true);
            itemBean.setZkb(1);
        } else if (item.getZkb() == 2) {
            rb2.setChecked(true);
            itemBean.setZkb(2);
        } else if (item.getZkb() == 3) {
            rb3.setChecked(true);
            itemBean.setZkb(3);
        }

        if (item.getWdlddj() > 0 && item.getWdlddj() <= spDatas.size()) {
            spinner_lddj.setSelection(item.getWdlddj() - 1);
        }

        setLlVisable(llSbkz, item.isWdkzxszt());

//        if (item.getClmc().contains("自定义"))
//            llSbkz.setVisibility(View.VISIBLE);
//        else
//            llSbkz.setVisibility(View.GONE);
//        "wdlddj": 3,
//                "sspl": 3,
//                "zkb": 1,
//                "wdkz": 2,
//                "wdld": 25,
//                "ldsc": 750,
//                "mdsc": 250,
//                "hdys": 0,
//                "mqys": 0,
//                "hsjsdzsl": 0,


    }

    private void setLlVisable(LinearLayout ll, boolean isShow) {
        if (isShow) {
            ll.setVisibility(View.VISIBLE);
        } else {
            ll.setVisibility(View.GONE);
        }
    }


    private void setListViewData() {
        if (null != datas && !datas.isEmpty()) {
            for (int i = 0; i < datas.size(); i++) {
                if (datas.get(i).getDm().equals(lightCode)) {
                    choosedPosition = i;
                }
            }

            spinnerSbkz.setSelection(choosedPosition + 1);


            adapter = new CommonAdapter<WorkModeItemBean>(datas, R.layout.item_layout_switch_button_item) {
                @Override
                public void convert(ViewHolder holder, WorkModeItemBean bean) {
                    int position = holder.getPosition();
                    holder.setText(R.id.tvName, bean.getDmsm());
                    boolean isChoosed = choosedPosition == position;
                    bean.setChoosed(isChoosed);
                    holder.setImageResource(R.id.ivChoose, isChoosed ? R.drawable.radio_checked : R.drawable.radio_unchecked);
                }
            };
            mListView.setAdapter(adapter);
        }


    }

    private boolean isHalf = false;

    private void setSpinnerData() {

        spAdapter = new CommonAdapter<WorkModeItemBean>(spDatas, R.layout.item_layout_spinner_item) {
            @Override
            public void convert(ViewHolder helper, WorkModeItemBean item) {

                helper.setText(R.id.tvName, item.getDm());
                if (isHalf) {
                    if (helper.getPosition() > 4) {
                        helper.getView(R.id.rl).setVisibility(View.GONE);
                    } else {
                        helper.getView(R.id.rl).setVisibility(View.VISIBLE);
                    }
                } else {
                    helper.getView(R.id.rl).setVisibility(View.VISIBLE);
                }

            }
        };
        spinner_lddj.setAdapter(spAdapter);

        spinner_lddj.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                //  tvWdld.setText(spDatas.get(position).getDmsm());
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


        for (int i = 0; i < spDatas.size(); i++) {
            if (spDatas.get(i).getDm().equals(itemBean.getWdlddj() + "")) {
                spinner_lddj.setSelection(i);
                break;
            }
        }


    }


    private static final String DEBUG_TAG = "debug_FogLightControllerActiv";

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.title_left:
                finish();
                break;
            case R.id.btnCommit:
//                if (spinnerSbkz.getSelectedItemPosition() == 0) {
//                    ToastUtil.showShort(mContext, "请选择雾灯闪烁状态");
//                    return;
//                }
                // WorkModeItemBean workModeItemBean = datas.get(choosedPosition);
                String token = UserInfoUtils.getInstance().getToken(mContext);

//                "wqbm":27,
//                    "gzms":1,
//                    "wdkz":1,
//                    "ldsc":250,
//                    "mdsc":500,
//                    "hdys":1,
//                    "mqys":2,
//                    "hsjsdzsl":3,
//                    "njddj":1,
//                    "sfds":1,
//                    "sdsc":5


//                    "wdld":25,
//                    "kzcl":1,
//                    "sspl":1,
//                    "zkb":1

                HashMap<String, Object> data = new HashMap<>();
                data.put("wqbm", itemBean.getWqbm());
                data.put("gzms", "2");
                //data.put("wdkz", spinnerSbkz.getSelectedItemPosition());
                if (llSbkz.getVisibility() == View.VISIBLE) {
                    if (spinnerSbkz.getSelectedItemPosition() == 0) {
                        ToastUtil.showShort(mContext, "请选择雾灯闪烁状态");
                        return;
                    }

                    itemBean.setWdkz(spinnerSbkz.getSelectedItemPosition());
                }


                data.put("wdkz", itemBean.getWdkz());
                data.put("ldsc", etLdsc.getText().toString());
                data.put("mdsc", etMdsc.getText().toString());
                data.put("hdys", etHdys.getText().toString());

                data.put("kzcl", itemBean.getKzcl());
                data.put("sspl", itemBean.getSspl());
                data.put("zkb", itemBean.getZkb());

                if (Integer.parseInt(etMqsc.getText().toString()) > 99) {
                    ToastUtil.showShort(mContext, "红色延时时间不超过99秒");
                    return;
                }
                data.put("mqys", etMqsc.getText().toString());
                if (etHsjsdsl.getVisibility() == View.VISIBLE)
                    data.put("hsjsdzsl", etHsjsdsl.getText().toString());
                else
                    data.put("hsjsdzsl", spinner3.getSelectedItem().toString());
                //data.put("wdlddj", itemBean.getNjddj());
                if (itemBean.isSfds())
                    data.put("sfds", 1);
                else
                    data.put("sfds", 0);
                data.put("cxsj", etSdsc.getText().toString());
                if (Integer.parseInt(tvWdld.getText().toString()) > 127 || Integer.parseInt(tvWdld.getText().toString()) < 1) {
                    ToastUtil.showShort(mContext, "雾灯亮度取值范围为1~127");
                    return;
                }
                data.put("wdld", tvWdld.getText().toString());
                if (itemBean.getSspl() == 1 && spinner_lddj.getSelectedItemPosition() > 4) {
                    ToastUtil.showShort(mContext, "常亮状态下亮度等级最大为5");
                    return;
                }
                data.put("wdlddj", spDatas.get(spinner_lddj.getSelectedItemPosition()).getDm());
                //雾区编号 dmlb 工作模式 dm

                showNormalDialog(token, data);
                // mPresenter.http_foglightWorkModeUpdate(mActivity, token, data);
                break;
        }
    }

    private void showNormalDialog(String token, HashMap<String, Object> data) {

        //雾区中至少需要有一个控制器设备


        if (areaControllers.size() == 0) {
            AlertDialog.Builder dialog =
                    new AlertDialog.Builder(mContext);
            dialog.setTitle("设备控制");
            dialog.setMessage("雾区中至少需要有一个控制器设备");
            dialog.setPositiveButton("确认",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });
            dialog.show();


        } else {

            String message = "控制策略:" + strategieEntry.getClmc() + "<br/>人工干预时长<font color='#ff0000'><big>" + etSdsc.getText().toString() + "分钟</big></font>";
            final AlertDialog.Builder normalDialog =
                    new AlertDialog.Builder(mContext);
            normalDialog.setTitle(itemBean.getWqmc());
            normalDialog.setMessage(Html.fromHtml(message));
            normalDialog.setPositiveButton("取消",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });
            normalDialog.setNegativeButton("确认",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            mPresenter.http_foglightWorkModeUpdate(mActivity, token, data);
                        }
                    });
            // 显示
            normalDialog.show();
        }
    }

    @Override
    protected void initPresenter() {
        mPresenter.setView(this);
    }

    @Override
    public void onError(CommException e) {
        ToastUtil.showShort(mContext, e.getMessage());
    }

    @Override
    public void onResponse(Object o) {
    }


    @Override
    public void getFogLightMode(List<WorkModeItemBean> workMeItemBeanList) {
        Log.e(TAG, "getFogLightMode: workMeItemBeanList===" + workMeItemBeanList);
        if (null != workMeItemBeanList && !workMeItemBeanList.isEmpty()) {
            spDatas.clear();

            spDatas.addAll(workMeItemBeanList);
            setSpinnerData();
        }

        HashMap<String, Object> data = new HashMap<>();
        data.put("wqbm", itemBean.getWqbm());

        mPresenter.http_getControllers(this, UserInfoUtils.getInstance().getToken(mContext), data);
    }

    @Override
    public void getFogLightModeError(CommException e) {
        ToastUtil.showShort(mContext, e.getMessage());
        finish();
    }

    private void initData() {
        datas.add(new WorkModeItemBean("1", "强制闪红灯"));
        datas.add(new WorkModeItemBean("2", "强制闪黄灯"));
        datas.add(new WorkModeItemBean("3", "检测来车红闪"));
        datas.add(new WorkModeItemBean("4", "关闭"));
        setListViewData();
    }


    private void setMode1() {
        etMdsc.setEnabled(false);
        etLdsc.setEnabled(false);
        etMqsc.setEnabled(false);
        etHdys.setEnabled(false);
        etHsjsdsl.setEnabled(false);

        etLdsc.setText("1500");
        etMdsc.setText("0");
        etMqsc.setText("2");
        etHdys.setText("15");

        spinner_sspl.setEnabled(false);
        spinner_sspl.setSelection(0);

        etHsjsdsl.setText("5");
        etHsjsdsl.setVisibility(View.VISIBLE);
        spinner3.setVisibility(View.GONE);
//        choosedPosition = 1;
//        adapter.notifyDataSetChanged();
        spinnerSbkz.setSelection(2);
        isHalf = true;
        spAdapter.notifyDataSetChanged();
        spinner_lddj.setSelection(1);

    }

    private void setMode2() {
        etMdsc.setEnabled(false);
        etLdsc.setEnabled(false);
        etMqsc.setEnabled(false);
        etHdys.setEnabled(false);
        etHsjsdsl.setEnabled(false);

        initSpinner();
        spinner_sspl.setSelection(2);
        etLdsc.setText("250");
        etMdsc.setText("750");
        etMqsc.setText("2");
        etHdys.setText("15");

        etHsjsdsl.setText("5");
        etHsjsdsl.setVisibility(View.VISIBLE);
        spinner3.setVisibility(View.GONE);
//        choosedPosition = 1;
//        adapter.notifyDataSetChanged();
        spinnerSbkz.setSelection(2);

        isHalf = false;
        spAdapter.notifyDataSetChanged();
        spinner_lddj.setSelection(1);
    }

    private void setMode3() {
        etMdsc.setEnabled(false);
        etLdsc.setEnabled(false);
        etMqsc.setEnabled(false);
        etHdys.setEnabled(true);
        etHsjsdsl.setEnabled(false);


        initSpinner();
        spinner_sspl.setSelection(1);

        etLdsc.setText("400");
        etMdsc.setText("1600");
        etMqsc.setText("2");
        etHdys.setText("1");

        etHsjsdsl.setVisibility(View.GONE);
        spinner3.setVisibility(View.VISIBLE);
        spinner3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0) {
                    etHdys.setEnabled(true);
                } else if (position < 4) {
                    etHdys.setEnabled(false);
                    etHdys.setText("15");
                } else {
                    etHdys.setEnabled(false);
                    etHdys.setText("30");
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        spinner3.setSelection(0);

        //        choosedPosition = 2;
//        adapter.notifyDataSetChanged();
        spinnerSbkz.setSelection(3);

        isHalf = false;
        spAdapter.notifyDataSetChanged();
        spinner_lddj.setSelection(1);
    }

    private void setMode4() {
        etMdsc.setEnabled(false);
        etLdsc.setEnabled(false);
        etMqsc.setEnabled(false);
        etHdys.setEnabled(false);
        etHsjsdsl.setEnabled(false);

        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.sspl2,
                android.R.layout.simple_spinner_dropdown_item);
        spinner_sspl.setAdapter(adapter);

        spinner_sspl.setEnabled(true);
        spinner_sspl.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        etLdsc.setText("1500");
                        etMdsc.setText("0");
                        isHalf = true;
                        spAdapter.notifyDataSetChanged();
                        break;
                    case 1:
                        etLdsc.setText("200");
                        etMdsc.setText("800");
                        isHalf = false;
                        spAdapter.notifyDataSetChanged();
                        break;
                    case 2:
                        etLdsc.setText("125");
                        etMdsc.setText("375");
                        isHalf = false;
                        spAdapter.notifyDataSetChanged();
                        break;
                    case 3:
                        etLdsc.setText("83");
                        etMdsc.setText("167");
                        isHalf = false;
                        spAdapter.notifyDataSetChanged();
                        break;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        ;

        isHalf = true;
        spAdapter.notifyDataSetChanged();
        spinner_lddj.setSelection(2);
        spinner_sspl.setSelection(0);


        etLdsc.setText("1500");
        etMdsc.setText("0");
        etMqsc.setText("2");
        etHdys.setText("15");

        etHsjsdsl.setText("5");
        etHsjsdsl.setVisibility(View.VISIBLE);
        spinner3.setVisibility(View.GONE);
        //        choosedPosition = 1;
//        adapter.notifyDataSetChanged();
        spinnerSbkz.setSelection(1);
    }


    private void setMode5() {
        etMdsc.setEnabled(true);
        etLdsc.setEnabled(true);
        etMqsc.setEnabled(true);
        etHdys.setEnabled(true);
        etHsjsdsl.setEnabled(true);
        etHsjsdsl.setVisibility(View.VISIBLE);
        spinner3.setVisibility(View.GONE);
        initSpinner();
        isHalf = false;
        spAdapter.notifyDataSetChanged();
    }

    int flag = 0;

    private void initSpinner() {
        ArrayAdapter<CharSequence> adapter;
        if (flag == 0) {
            adapter = ArrayAdapter.createFromResource(
                    this, R.array.sspl,
                    android.R.layout.simple_spinner_dropdown_item);
        } else {
            adapter = ArrayAdapter.createFromResource(
                    this, R.array.sspl2,
                    android.R.layout.simple_spinner_dropdown_item);
        }
        spinner_sspl.setAdapter(adapter);

        spinner_sspl.setEnabled(true);


        spinner_sspl.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (flag == 0) {
                    itemBean.setSspl(position + 2);
                } else if (flag == 1) {
                    if (position == 0) {
                        itemBean.setSspl(position + 1);
                    } else {
                        itemBean.setSspl(position + 2);
                    }
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


    }

    @Override
    public void updtaeFogLightMode(ArrayList<UpdateWorkModeResultBean.Item2Bean> resultBean) {

        if (resultBean != null) {
            //ToastUtil.showShort(mContext, "操作成功");
            showResult(resultBean);
        } else {
            ToastUtil.showShort(mContext, "操作失败，请联系系统管理员！");
        }

    }


    private void showResult(ArrayList<UpdateWorkModeResultBean.Item2Bean> resultBean) {

        // String[] items = new String[resultBean.size()];
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < resultBean.size(); i++) {
            // items[i] = "控制器" + resultBean.get(i).getKey() + "：" + getResult(resultBean.get(i).getCzjg());
            stringBuffer.append("控制器" + resultBean.get(i).getKey() + "：" + getResult(resultBean.get(i).getCzjg())).append("\n");
        }

        AlertDialog.Builder listDialog =
                new AlertDialog.Builder(mContext);
        listDialog.setTitle(itemBean.getWqmc());
        listDialog.setMessage("处理结果：\n" + stringBuffer.toString());
//        listDialog.setItems(items, null);
        listDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });
        listDialog.show();

    }

    private String getResult(int cljg) {
        switch (cljg) {
            case 0:
                return "成功";
            case 1:
                return "发送失败";
            case 2:
                return "发送超时";
            case 3:
                return "连接异常";
            case 4:
                return "未找到连接";
            default:
                return "未知结果";
        }
    }


    @Override
    public void getStrategiesResult(ArrayList<StrategieEntry> strategies) {
        kzclData.clear();
        kzclData.addAll(strategies);
        kzclAdapter.notifyDataSetChanged();
        mPresenter.getFoglightMode(this, UserInfoUtils.getInstance().getToken(mContext));
    }


    List<AreaControllerDetailBean> areaControllers = new ArrayList<>();

    @Override
    public void getControllersResult(ArrayList<AreaControllerDetailBean> controllers) {
        areaControllers.clear();
        areaControllers.addAll(controllers);
    }

    @Override
    public void getStrategiesError(CommException e) {
        ToastUtil.showShort(mContext, e.getMessage());
        finish();
    }


}
